package com.ygqh.baby.service.task.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.ao.TaskMethod;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.service.YgTaskTimeService;
import com.ygqh.baby.service.YgTaskTimeTemplate;
import com.ygqh.baby.service.linkstars.YgLinkStarsService;
import com.ygqh.baby.service.linkstars.model.LSResult;
import com.ygqh.baby.service.linkstars.model.LSResultData;
import com.ygqh.baby.service.task.YgLSTaskService;
import com.ygqh.baby.utils.DateConvertUtils;

@Service
public class YgLSTaskServiceImpl implements YgLSTaskService {
	protected final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private YgLinkStarsService ygLinkStarsService;
	@Autowired
	private YgTaskTimeService ygTaskTimeService;

	@Override
	public void orderPushTask(int timeInterval) {

		ygTaskTimeService.execute(TaskMethod.LsOrderPushTask, timeInterval, new YgTaskTimeTemplate() {

			@Override
			public void execute(Date runTime) {
				Date endTime = runTime;
				Date startTime = DateConvertUtils.addMinute(runTime, -timeInterval);
				String sign = "linkStars";
				List<YgOrder> cancelOrders = ygLinkStarsService.getLSOrderByTime(startTime, endTime, OrderStatus.TradeClosed, sign);
				List<YgOrder> completeOrders = ygLinkStarsService.getLSOrderByTime(startTime, endTime, OrderStatus.TradeSuccess, sign);
				List<YgOrder> sendOrders = ygLinkStarsService.getLSOrderByTime(startTime, endTime, OrderStatus.WaitingSign, sign);
				List<YgOrder> paidOrders = ygLinkStarsService.getLSOrderByTime(startTime, endTime, OrderStatus.WaitingSend, sign);
				cancelOrders.addAll(completeOrders);
				cancelOrders.addAll(sendOrders);
				// . 订单信息推送接口
				List<Long> orderPush = new ArrayList<Long>();
				List<Long> effectPush = new ArrayList<Long>();
				paidOrders.forEach(o -> {
					LSResult lsResult = ygLinkStarsService.orderPush(o);
					orderPush.add(o.getOrderId());
					if (lsResult.getCode() != 1) {
						logger.warn(o.getOrderId() + ";" + lsResult.getMsg());
					}
				});
				// 订单状态推送接口
				cancelOrders.forEach(o -> {
					LSResult lsResult = ygLinkStarsService.effectPush(cancelOrders);
					effectPush.add(o.getOrderId());
					if (lsResult.getCode() != 1) {
						logger.warn(lsResult.getMsg());
					} else {
						List<LSResultData> data = lsResult.getData();
						data.forEach(d -> {
							if (d.getResult() != 1) {
								logger.warn(lsResult.getMsg() + ";【" + d.getOrderNumber() + "】【" + d.getMsg() + "】");
							}
						});

					}
				});
				if (orderPush.size() > 1) {
					logger.info("【orderPush】" + orderPush.toString());
				}
				if (effectPush.size() > 1) {
					logger.info("【effectPush】" + effectPush.toString());
				}
				//ygTaskTimeService.saveOrUpdate(TaskMethod.LsOrderPushTask.name(), runTime, "正常");
			}
		});

	}

}
